package com.cloud.punish.interfaces;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.feign.FeignClient;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.cloud.punish.api.bean.LawCauseClDetail;
import com.cloud.punish.api.bean.LawCauseItem;
import com.cloud.punish.api.bean.LawCauseLaw;
import com.cloud.punish.api.bean.LawCauseLawItem;
import com.cloud.punish.api.bean.LawCauseRelCauseExcuteType;
import com.cloud.punish.api.bean.LawCauseSearchCriteria;
import com.cloud.punish.common.config.FeignConfiguration;
import com.github.pagehelper.PageInfo;

import feign.hystrix.FallbackFactory;

import java.util.List;

@FeignClient(value = "LAWCASE", configuration = FeignConfiguration.class, fallbackFactory = FeignClientLawCaseFallbackFactory.class)
public interface FeignClientLawCase {
	@RequestMapping(value = "lawcase/interface/causesearch", method = RequestMethod.POST)
	public PageInfo<LawCauseItem> lawCausePagination(@RequestBody LawCauseSearchCriteria criteria);

	@RequestMapping(value = "lawcase/interface/listExceuteTypeByCauseCode", method = RequestMethod.GET)
	List<LawCauseRelCauseExcuteType> listExceuteTypeByCauseCode(List<String> causeCodes);

	@RequestMapping(value = "lawcase/interface/listClDetailByCauseCode", method = RequestMethod.GET)
	List<LawCauseClDetail> listClDetailByCauseCode(List<String> causeCodes);

	@RequestMapping(value = "lawcase/interface/searchlaw", method = RequestMethod.GET)
	public List<LawCauseLaw> searchLaw(@RequestParam("title")String title, @RequestParam("type")int type);
	
	@RequestMapping(value = "lawcase/interface/searchlawitem", method = RequestMethod.GET)
	public List<LawCauseLawItem> searchLawItem(@RequestParam("lawId")Long lawId, @RequestParam("type")int type);

	@RequestMapping(value = "lawcase/interface/listCasueIDByCauseCode", method = RequestMethod.GET)
	List<LawCauseItem> listCasueIDByCauseCode(List<String> causeCodes);
}

@Component
class FeignClientLawCaseFallbackFactory implements FallbackFactory<FeignClientLawCase> {
	private static final Logger LOGGER = LoggerFactory.getLogger(FeignClientLawCaseFallbackFactory.class);
	
	@Override
	public FeignClientLawCase create(Throwable cause) {
		return new FeignClientLawCase(){
			@Override
			public PageInfo<LawCauseItem> lawCausePagination(LawCauseSearchCriteria criteria) {
				if (FeignClientLawCaseFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientLawCaseFallbackFactory.LOGGER
							.debug("lawCausePagination fallback; reason was:" + cause);
				}
				return null;
			}

			@Override
			public List<LawCauseRelCauseExcuteType> listExceuteTypeByCauseCode(List<String> causeCodes) {
				if (FeignClientLawCaseFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientLawCaseFallbackFactory.LOGGER
							.debug("lawCausePagination fallback; reason was:" + cause);
				}
				return null;
			}

			@Override
			public List<LawCauseClDetail> listClDetailByCauseCode(List<String> causeCodes){
				if (FeignClientLawCaseFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientLawCaseFallbackFactory.LOGGER
							.debug("lawCausePagination fallback; reason was:" + cause);
				}
				return null;
			}

			@Override
			public List<LawCauseLaw> searchLaw(String title, int type) {
				if (FeignClientLawCaseFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientLawCaseFallbackFactory.LOGGER
							.debug("searchLaw fallback; reason was:" + cause);
				}
				return null;
			}

			@Override
			public List<LawCauseLawItem> searchLawItem(Long lawId, int type) {
				if (FeignClientLawCaseFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientLawCaseFallbackFactory.LOGGER
							.debug("searchLawItem fallback; reason was:" + cause);
				}
				return null;
			}

			@Override
			public List<LawCauseItem> listCasueIDByCauseCode(List<String> causeCodes) {
				if (FeignClientLawCaseFallbackFactory.LOGGER.isDebugEnabled()) {
					FeignClientLawCaseFallbackFactory.LOGGER
							.debug("searchLawItem fallback; reason was:" + cause);
				}
				return null;
			}

		};
	}
	
}